package com.ming.closeto.ui;


import android.hardware.Camera;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.ming.closeto.R;
import com.ming.closeto.base.BaseActivity;

import java.io.IOException;

public class CompassActivity extends BaseActivity implements SurfaceHolder.Callback {

    private SurfaceView mSurfaceView;
    private ImageView iv_point;
    private TextView tv_angle;

    private SensorManager mSensorManager;
    private EventListener mEventListener;

    private SurfaceHolder holder;
    private Camera mCamera;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_compass);
        initView();

        // 方向传感器 + 相机
    }

    private void initView() {
        mSurfaceView = (SurfaceView) findViewById(R.id.mSurfaceView);
        iv_point = (ImageView) findViewById(R.id.iv_point);
        tv_angle = (TextView) findViewById(R.id.tv_angle);

        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        mEventListener = new EventListener();
        mSensorManager.registerListener(mEventListener, sensor, mSensorManager.SENSOR_DELAY_FASTEST);

        holder = mSurfaceView.getHolder();
        holder.addCallback(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(mCamera ==null){
            mCamera = getCamera();
            if(holder != null){
                showCamera(mCamera,holder);
            }
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        showCamera(mCamera,holder);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        mCamera.stopPreview();
        showCamera(mCamera,holder);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        clearCamera();
    }

    private  Camera getCamera(){
        Camera camera = null;
        try {
            camera = Camera.open();
        }catch (Exception e){

        }
        return camera ;
    }

    private void showCamera(Camera mCamera, SurfaceHolder holder) {
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.setDisplayOrientation(90);
            mCamera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void clearCamera() {
        if(mCamera != null){
            mCamera.stopPreview();
            mCamera.setPreviewCallback(null);
            mCamera.release();
            mCamera = null;
        }
    }


    class EventListener implements SensorEventListener {

        float lastValus = 0;

        @Override
        public void onSensorChanged(SensorEvent event) {
            float values = event.values[0];
            RotateAnimation rotateAnimation  = new RotateAnimation(-lastValus,values, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f);
            iv_point.startAnimation(rotateAnimation);
            lastValus = values;
            /**
             * 东0南90西180北270
             */
            if (values > 0 && values < 90) {
                tv_angle.setText("东：" + (360-values) + "度");
            } else if (values > 90 && values < 180) {
                tv_angle.setText("南：" + (360-values) + "度");
            } else if (values > 180 && values < 270) {
                tv_angle.setText("西：" + (360-values) + "度");
            } else if (values > 270 ) {
                tv_angle.setText("北：" + (360-values) + "度");
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mSensorManager.unregisterListener(mEventListener);
    }
}
